package com.vader.boot.aspect;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author Vader
 * @date 2022/5/25
 **/
@Slf4j
public class JdkProxyTest {

    interface Foo {
        void foo() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException;

        int bar() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException;
    }

    interface InvocationHandler {
        Object invoke(Object proxy,Method method, Object[] objects) throws InvocationTargetException, IllegalAccessException;
    }

    static class Target implements Foo {

        @Override
        public void foo() {
            log.info("target_foo");
        }

        @Override
        public int bar() {
            log.info("target_bar");
            return 100;
        }
    }

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Foo foo = new $JdkProxyTest((Object proxy,Method method, Object[] objects) -> {
            log.info("$JdkProxyTest_invoke");
//            new Target().foo();
            Object invoke = method.invoke(new Target(), objects);
            log.info("invoke:{}",invoke);
            return invoke;
        });
        foo.foo();
        foo.bar();
    }
}
